package com.heima.common.minio.config;

import com.heima.common.minio.properties.MinIOProperties;
import com.heima.common.minio.service.MinIOService;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author: itheima
 * @create: 2021-12-23 09:49
 */
@Configuration
@EnableConfigurationProperties(MinIOProperties.class)
@Import({MinIOService.class})
@ConditionalOnProperty(prefix = "minio", value = "enable", havingValue = "true")  //当配置文件中包含某个属性，才进行加载
public class MinIOConfig {

    /**
     * 注册访问MinIOn客户端对象
     *
     * @param properties 通过构造注入IOC容器中属性类对象
     * @return
     */
    @Bean
    public MinioClient minioClient(MinIOProperties properties) {
        //1.设置MinIOn服务URL地址端口，访问key 访问秘钥
        MinioClient client = MinioClient.builder().endpoint(properties.getEndpoint())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();
        return client;
    }
}
